package com.zz.framework.core

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import com.zz.framework.core.utils.XPreference
import com.zz.framework.core.utils.language.MultiLanguages
import com.zz.framework.core.utils.language.OnLanguageListener
import java.util.*
import kotlin.properties.Delegates

/**
 * Copyright © 2019 zanezhao All rights reserved.
 * Created by zanezhao on 2019-11-13 16:14.
 *
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */
open class BaseApp : Application() {

    companion object {
        var context: Context by Delegates.notNull()
    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        // 设置语种变化监听器
        MultiLanguages.setOnLanguageListener(object : OnLanguageListener {
            override fun onAppLocaleChange(oldLocale: Locale, newLocale: Locale) {
//                Log.d("MultiLanguages", "监听到应用切换了语种，旧语种：$oldLocale，新语种：$newLocale")
            }

            override fun onSystemLocaleChange(oldLocale: Locale, newLocale: Locale) {
//                Log.d("MultiLanguages", "监听到系统切换了语种，旧语种：" + oldLocale + "，新语种：" + newLocale + "，是否跟随系统：" + MultiLanguages.isSystemLanguage())
            }
        })
        XPreference.init(this)
//        AutoDensity.instance.init(this, DesignDraft(designSize = 375f))
    }


    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale !== 1f) //非默认值
            resources
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources? {
        val res = super.getResources()
        if (res.configuration.fontScale != 1f) { //非默认值
            val newConfig = Configuration()
            newConfig.setToDefaults() //设置默认
            res.updateConfiguration(newConfig, res.displayMetrics)
        }
        return res
    }
}
